<?php
/*  Copyright 2010  webprodigy.ca (email : info@webprodigy.ca)
    This file is part of Download Protect.

    Download Protect is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Download Protect is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Download Protect.  If not, see <http://www.gnu.org/licenses/>.

*/
require_once ('filedownloader.php');
require_once('../../../wp-load.php');

class DownloadProtectDownloader {
	
	public function __construct() {
		$this->dlprotect_Download();
		//echo $this->dlprotect_Decrypt($_GET['d']);
	}
	public function dlprotect_Download() {
		//global $current_user, $user_ID;
		if($this->dlprotect_Decrypt($_GET['v']) == 'private') {
			if (is_user_logged_in()){
				$downloadfile = $this->dlprotect_Decrypt($_GET['d']);
				$loader = new FileDownloader($downloadfile);
				$loader->download();
			}
			else header('Location: /');
		}
		else {
			$downloadfile = $this->dlprotect_Decrypt($_GET['d']);
			$loader = new FileDownloader($downloadfile);
			$loader->download();
		}
	}
	
	private function dlprotect_Decrypt($text) {
		$key = get_option('dlprotect_encrypt_key');
		$string = urldecode($text);
		for($i=1; $i<=strlen($string); $i++) {
			$char = substr($string, $i-1, 1);
			$keychar = substr($key, ($i % strlen($key))-1, 1);
			$char = chr(ord($char)-ord($keychar));
			$result.=$char;
		}
		return $result;
	}
}
$downloader = new DownloadProtectDownloader();